﻿

var circleConfig = {
    M001: "请填写圈子标签",
    M002: "标签长度不能超过100",
    M003: "圈子标签超过5个，请重新输入",
    M004: "退出圈子失败",
    M005: "退出圈子成功",
    DismissCircleFail: "解散圈子失败",
    ReplySuccessed: "评论成功",
    ReplyFail: "评论失败",
    JoinSuccessed: "加入圈子成功" ,
    JoinCircleFail: "加入圈子失败",
    ApplyJoinSuccessed: "申请成功",
    ApplyJoinCircleFail: "申请失败",
    DeleteReplySuccessed: "删除回复成功",
    DeleteReplyFail: "删除回复失败",
    EditQuestionSuccessed: "编辑提问成功",
    EditQuestionFail: "编辑提问失败",
    ExitSuccessed:{state:true,msg:"退出圈子成功"} ,
    ExitCircleFail: "退出圈子失败",
    DismissSuccessed:{state:true,msg:"解散圈子成功"},
    DismissCircleFail: "解散圈子失败",
    AttentionSuccess: { state: true, msg: "关注成功" },
    AttentionFail:"关注未成功"
};

//圈子模块
DaAn.Circle = {
    Url: {
        Jion: "joinCircle",
        Exit: "joinCircle"
    },
    circleConfig :{
        M001: "请填写圈子标签",
        M002: "标签长度不能超过100",
        M003: "圈子标签超过5个，请重新输入",
        M004: "退出圈子失败",
        M005: "退出圈子成功"
    },
    GetUrl: function (url) {
        return "/Services/AjaxHandler.ashx?action=Circle." + url;
    },
    JoinTips: function (type, result) {
        /// <summary>
        /// 加入圈子提示
        /// </summary>
        /// <param name="type"> 
        ///  加入类型：1、直接加入圈子提示  2、审核验证加入圈子提示
        /// </param> 
        /// <param name="result"> 
        ///  成功/失败
        /// </param> 
        if (type == 1) {

            if (result) {
                DaAn.Tips({ type: "load", content: circleConfig["JoinSuccessed"] });
                location = location;
            } else {
                DaAn.Tips({ type: "info", content: circleConfig["JoinCircleFail"] });
            }
        } else if (type == 2) {
            if (result) {
                DaAn.Tips({ type: "load", content: circleConfig["ApplyJoinSuccessed"] });
                location = location;
            } else {
                DaAn.Tips({ type: "info", content: circleConfig["ApplyJoinCircleFail"] });
            }
        }
    },
    Join: function (type, circleID, content, callback) {
        /// <summary>
        /// 加入圈子
        /// </summary>
        /// <param name="type"> 
        ///  加入类型：1、直接加入  2、审核验证加入
        /// </param> 
        /// <param name="circleID"> 
        ///  圈子ID
        /// </param> 
        /// <param name="content"> 
        ///  审核内容
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.joinCircle");
        if (type == 1) {
            DaAn.Ajax(url, { cid: circleID }, function (result) {
                if (typeof callback == "function") { callback(circleConfig[result], result == "1013" ? true : false); }
            }, { type: "post" });
        } else if (type == 2) {
            DaAn.Ajax(url, { cid: circleID, con: content }, function (result) {
                if (typeof callback == "function") { callback(circleConfig[result], result == "1013" ? true : false); }
            }, { type: "post" });
        }
    },
    Exit: function (circleID, callback) {
        /// <summary>
        /// 退出圈子
        /// </summary>
        /// <param name="circleID"> 
        ///  圈子ID
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.exitCircle");
        DaAn.Ajax(url, { circleid: circleID }, function (result) {
            if (typeof callback == "function") {
                callback(circleConfig[result]);
            }
        }, { type: "post" });
        //        DaAn.Common.Ajax(
        //            DaAn.Common.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.exitCircle"),
        //            { circleid: circleID },
        //            function (result) {
        //                if (typeof callback == "function") {
        //                    callback(circleConfig[result], result == "Successed" ? true : false);
        //                }
        //            },
        //            { type: "post" }
        //        );
    },
    Dismiss: function (circleID, callback) {
        /// <summary>
        /// 解散圈子
        /// </summary>
        /// <param name="circleID"> 
        /// 圈子ID
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.dismissCircle");
        DaAn.Ajax(url, { circleid: circleID }, function (result) {
            if (typeof callback == "function") {
                callback(circleConfig[result]);
            }
        }, { type: "post" });
    },
    Reply: function (title, content, circleID, questionID, callback) {
        /// <summary>
        /// 回复帖子
        /// </summary>
        /// <param name="title"> 
        ///  帖子内容
        /// </param> 
        /// <param name="content"> 
        ///  回复内容
        /// </param> 
        /// <param name="circleID"> 
        ///  圈子ID
        /// </param> 
        /// <param name="questionID"> 
        ///  问题ID
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.publishReply");
        DaAn.Ajax(url, { t: title, content: content, circleid: circleID, questionid: questionID }, function (result) {
            if (typeof callback == "function") { callback(circleConfig[result], result == "ReplySuccessed" ? true : false); }
        }, { type: "post" });
    },
    DeleteReply: function (ReplyId, callback) {
        /// <summary>
        /// 删除回复
        /// </summary>
        /// <param name="ReplyId"> 
        ///  回复ID
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.deleteComment");
        DaAn.Ajax(url, { ReplyId: ReplyId }, function (result) {
            if (typeof callback == "function") { callback(circleConfig[result], result == "DeleteReplySuccessed" ? true : false); }
        }, { type: "post" });
    },
    EditQuestion: function (userid, edittitle, questionid, callback) {
        /// <summary>
        /// 提问编辑（只有身份为医生并且是医生本人的回复）
        /// </summary>
        /// <param name="userid

        ///  用户ID
        /// </param> 
        /// <param name="edittitle"> 
        ///  编辑内容
        /// <param name="questionid"> 
        ///  帖子ID
        /// </param> 
        var url = DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.editQuestion");
        DaAn.Ajax(url, { userid: userid, title: edittitle, questionid: questionid }, function (result) {
            if (typeof callback == "function") { callback(circleConfig[result], result == "EditQuestionSuccessed" ? true : false); }
        }, { type: "post" });
    },
    CheckRead: function (obj, callback) {  //检查用户是否勾选已经阅读一些协议
        if ($(obj).attr("checked") === "checked") {
            callback.call(this, true, "");
        } else {
            callback.call(this, false, "<span class='error-icon margin-l10'></span>请接受服务条款");
            return;
        }
    },

    CheckName: function (name, oldName, userid, callback) { //验证圈子名称
        if (name.length == 0) {
            callback.call(this, false, "请填写圈子名称");
            return;
        } else {
            if (name.length > 16) {
                callback.call(this, false, "字符长度不能大于16");
                return;
            }
            if (oldName == name) {
                callback.call(this, true, "");
                return;
            }
            var url = "/Services/AjaxHandler.ashx?action=Circle.GetCreateCircle";
            DaAn.Ajax(url, { uid: userid, name: name }, function (result) {
                if (result == "1010") {  //存在
                    callback.call(this, false, "该圈子名称已经存在，请换一个试试");
                    return;
                } else if (result == "1001") {
                    callback.call(this, false, "发生异常");
                    return;
                }
                else {
                    callback.call(this, true, "");
                }

            }, { type: "post" });
        }

    },

    Modify: function () {

    },
    Search: function (data, callback) {
        /// <summary>
        /// 热门圈子分页
        /// </summary>
        /// <param name="data"> 
        ///  分页参数 
        /// </param> 
        var defaults = { key: "", tagName: "", pageIndex: 1, pageSize: 12 };
        DaAn.Ajax(
            DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.GetCircleList"),
            $.extend(defaults, data || {}),
            function (result) {
                if (typeof callback == "function") {
                    callback(result);
                }
            },
            { type: "post" }
        );
    },
    SearchByTag: function (data, callback) {
        /// <summary>
        /// 热门圈子分页
        /// </summary>
        /// <param name="data"> 
        ///  分页参数 keyword orderBy categoryId tagId  pageIndex 
        /// </param> 
        var defaults = { keyword: "", orderBy: "", categoryId: -1, tagId: -1, pageIndex: 0, pageSize: 1, type: "" };
        DaAn.Ajax(
            DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.GetCircleListTag"),
            $.extend(defaults, data || {}),
            function (result) {
                if (typeof callback == "function") {
                    callback(result);
                }
            },
            { type: "post" }
        );
    },
    CheckTag: function (tag, callback) {  //验证圈子标签
        if (tag.length == 0) {
            circleConfig.DismissCircleFail
            callback.call(this, false, "请填写圈子标签");
            return;
        } else {
            if (tag > 10) {
                callback.call(this, false, "标签长度不能超过100");
                return;
            } else {
                var array = tag.split(" ");
                var nullArray = new Array();
                for (var i = 0; i < array.length; i++) {
                    if (array[i] != null && array[i] != "") {
                        nullArray.push(array[i]);
                    }
                }
                if (nullArray.length > 5) {
                    callback.call(this, false, "圈子标签超过5个，请重新输入");
                    return;
                }
            }
            callback.call(this, true, "");
        }
    },
    CreateCircle: function () { //创建圈子

    },
    ModifyCircle: function () { //修改圈子


    },
    GetTagListByCategoryId: function (categoryId, callback) {
        DaAn.Ajax(
            DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.GetTagListByCategoryId"),
            { categoryId: categoryId },
            function (result) {
                if (typeof callback == "function") {
                    callback(result);
                }
            },
            { type: "post" }
        );
    },
    AddAttention: function (objId, callback) {
        /// <summary>
        /// 加关注
        /// </summary>
        /// <param name="objId"> 
        ///  分页参数 关注对象ID
        /// </param> 
        DaAn.Ajax(
            DaAn.Http.AddRandomNumber("/Services/AjaxHandler.ashx?action=Circle.AddAttention"),
            { ObjId: objId },
            function (result) {
                if (typeof callback == "function") {
                    callback(circleConfig[result]);
                }
            },
            { type: "post" }
        );
    }
}
